package com.example.myapplication10__4;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 注册上下文菜单到两个控件
        registerForContextMenu(findViewById(R.id.iv_panda));
        registerForContextMenu(findViewById(R.id.tv_poem));
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        // 根据不同的控件加载不同的菜单
        if (v.getId() == R.id.iv_panda) {
            getMenuInflater().inflate(R.menu.image_menu, menu);
            menu.setHeaderTitle("图片操作");
            menu.setHeaderIcon(R.drawable.icon2); // 设置标题图标
        } else if (v.getId() == R.id.tv_poem) {
            getMenuInflater().inflate(R.menu.text_menu, menu);
            menu.setHeaderTitle("文本操作");
            menu.setHeaderIcon(R.drawable.icon2);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return true;
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.save_image) {
            Toast.makeText(this, "保存图片", Toast.LENGTH_SHORT).show();
            return true;
        } else if (id == R.id.view_image) {
            Toast.makeText(this, "查看图片", Toast.LENGTH_SHORT).show();
            return true;
        } else if (id == R.id.copy_text) {
            Toast.makeText(this, "复制文本", Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.onContextItemSelected(item);
    }
}